<div id="main">
    <div id="contents">
        <form method="post" action="#" name="filters" id="form1">
            <fieldset>
                <legend>
                    <label sid="ports.basic_setting"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%">
                        <tbody>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="id2324">
                                    <label sid="ports.basic_filter"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id2324_1" type="radio" value="1" name="id2324">
                                <label sid="main.enable"></label>

                                <span style="padding-left:20px;"></span>
                                <input id="id2324_2" type="radio" checked="" value="0" name="id2324">
                                <label sid="main.disable"></label>
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">&nbsp;</td>
                            <td class="frame_tab_right"></td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="id2323">
                                    <label sid="ports.basic_default_policy"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id2323_1" type="radio" value="1" name="id2323">
                                <label sid="ports.basic_default_policy_dropped"></label>
                                <br>
                                <br>
                                <input id="id2323_2" type="radio" value="0" name="id2323">
                                <label sid="ports.basic_default_policy_accepted"></label>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"></td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center" colspan="2">
                                <input class="button" type="button" name="submit" sid="sbutton.save">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <fieldset>
                <legend>
                    <label sid="ports.filter_setting"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%">
                        <tbody>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="action">
                                    <label sid="ports.filter_action"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <select id="action" name="action">
                                    <option id="portFilterActionDrop" value="0" sid="ports.filter_action_drop"></option>
                                    <option id="portFilterActionAccept" value="1" sid="ports.filter_action_accept"></option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="comment">
                                    <label sid="firewall.comment"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="comment" type="text" maxlength="32" size="16" name="comment">
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="mac_address">
                                    <label sid="ports.filter_macaddr"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="mac" type="text" name="mac" size="18">
                                <span class="x-form-tip" id="macTip"></span>

                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="dip_address">
                                    <label sid="ports.filter_dest_ipaddr"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="dip" type="text" name="dip" size="16">
                                <span class="x-form-tip" id="dipTip"></span>

                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="sip_address">
                                    <label sid="ports.filter_source_ipaddr"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="sip" type="text" name="sip" size="16">
                                <span class="x-form-tip" id="sipTip"></span>

                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="protocol">
                                    <label sid="firewall.protocol"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <select id="protocol" name="protocol">
                                    <option value="5">ALL</option>
                                    <option value="1">TCP</option>
                                    <option value="2">UDP</option>
                                    <option value="4">ICMP</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="dFromPort">
                                    <label sid="ports.filter_dest_port_range"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="dFromPort" type="text" name="dFromPort" size="5">
                                -
                                <input id="dToPort" type="text" name="dToPort" size="5">
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="sFromPort">
                                    <label sid="ports.filter_source_port_range"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="sFromPort" type="text" name="sFromPort" size="5">
                                -
                                <input id="sToPort" type="text" name="sToPort" size="5">
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"></td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center" colspan="2">
                                <input id="save_button" class="button" type="button" name="closetietrun" sid="sbutton.add"/>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <fieldset>
                <legend>
                    <label sid="ports.current_filter"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table id="port_filter" class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%"> </table>
                </div>
            </fieldset>
        </form>
    </div>
</div>
<br>
<div id="helpContainer">
    <div id="help">
        <div>
            <h2>
                <label sid="share.help"></label>
            </h2>
        </div>
        <div id="helpBody">
        <dl>
            <dt class="term">
                <label sid="ports.basic_filter"></label>
            </dt>
            <dd class="definition">
                <label sid="ports.basic_filter_note"></label>
            </dd>
            <dt class="term">
                <label sid="ports.filter_setting"></label>
            </dt>
            <dd class="definition">
                <label sid="ports.filter_setting_note"></label>
            </dd>


        </dl>

    </div>
    </div>
</div>
<script type="text/javascript" src="js/libTable.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
         var isSave=false;
        $.formValidator.initConfig({formID:"form1",debug:false,submitOnce:true,submitButtonID:"save_button",
            onError:function(msg, obj, errorlist) {

            },
            onSuccess:function(){
                var mac = $("#mac").val().replace(new RegExp("-","g"),":");
                var dip = $("#dip").val();
                var sip = $("#sip").val();
                if(dip==""){
                    dip="any/0";
                }
                if(sip==""){
                    sip="any/0";
                }
                var protocol = $("#protocol").val();
                var dfromport = $("#dFromPort").val();
                var dtoport = $("#dToPort").val();
                var sfromport = $("#sFromPort").val();
                var stoport = $("#sToPort").val();
                var action=$("#action").val();
                var comment=$("#comment").val();
                if($("#save_button").val()==sbutton.modify){
                    virtual_data[old_virtual][11]=mac;
                    virtual_data[old_virtual][4]=dip;
                    virtual_data[old_virtual][0]=sip;
                    virtual_data[old_virtual][8]=protocol;
                    virtual_data[old_virtual][6]=dfromport;
                    virtual_data[old_virtual][7]=dtoport;
                    virtual_data[old_virtual][2]=sfromport;
                    virtual_data[old_virtual][3]=stoport;
                    virtual_data[old_virtual][9]=action;
                    virtual_data[old_virtual][10]=comment;
                }else{
                    var newarray= new Array();
                    newarray[11]=mac;
                    newarray[4]=dip;
                    newarray[0]=sip;
                    newarray[8]=protocol;
                    newarray[6]=dfromport;
                    newarray[7]=dtoport;
                    newarray[2]=sfromport;
                    newarray[3]=stoport;
                    newarray[9]=action;
                    newarray[10]=comment;
                    newarray[1]='';
                    newarray[5]='';
                    virtual_data.push(newarray);
                    $("#save_button").val(sbutton.add);
                }
                var resultStr = JSONTostr(virtual_data);
                var settext='&id2325='+resultStr;
                submitform(modules["set_data"],settext,"port_filtering_main_load");
                load_html();
            }
        });
        $("#mac").formValidator({
            onFocus:errmsg.Info7
        }).regexValidator({
                    regExp:regexEnum.notempty,
                    onError:errmsg.Info3
                }).regexValidator({
                    regExp:regexEnum.mac,
                    onError:errmsg.Info1
                });
        $("#dip").formValidator({
            onFocus:errmsg.Info9
        }).regexValidator({
                    regExp:regexEnum.notempty,
                    onError:errmsg.Info3
                }).regexValidator({
                    regExp:regexEnum.ip4,
                    onError:errmsg.Info1
                });

        $("#sip").formValidator({
            onFocus:errmsg.Info9
        }).regexValidator({
                    regExp:regexEnum.notempty,
                    onError:errmsg.Info3
                }).regexValidator({
                    regExp:regexEnum.ip4,
                    onError:errmsg.Info1
                });
        $("#mac,#dip,#sip").unFormValidator(true);
        $("#mac,#dip,#sip").bind("blur",function(){
            if($(this).val()==""){
                if(!isSave){
                $(this).unFormValidator(true);
                }
            }
        });
        $("#mac,#dip,#sip").bind("click",function(){
            isSave =false;
            $(this).unFormValidator(false);
        });
        $("#save_button").bind("mouseover",function(){

             if($("#mac").val()==""&&$("#dip").val()==""&&$("#sip").val()==""){
                  $("#mac").unFormValidator(false);
                 return false;
             }
        });
        $("#save_button").bind("click",function(){
            isSave =true;
        })

    });
	var virtual_data = new Array();
	var old_virtual=0;
load_html();
$("[name=submit]").click(function () {
		$(".errormsg").html('');
		var settext='';
		if($("#id2323_1").attr("checked")){
			settext+="&id2323="+$("#id2323_1").val();
			}
			if($("#id2323_2").attr("checked")){
			settext+="&id2323="+$("#id2323_2").val();
			}
		if($("#id2324_1").attr("checked")){
			settext+="&id2324="+$("#id2324_1").val();
			}
			if($("#id2324_2").attr("checked")){
			settext+="&id2324="+$("#id2324_2").val();
			}
		submitform(modules["set_data"],settext,"port_filtering_main_load");
	});
	
function load_html(){
	var sendtext = '&id=2323&id=2324&id=2325';
	DataRequest("get_json",sendtext,function(data){
		updatecfgvalue("id2323",data["id2323"]);
		updatecfgvalue("id2324",data["id2324"]);
		var tmparray = strToJSON(data["id2325"]);
		//if(tmparray!=""&&tmparray){
		virtual_data = tmparray;
		virtual_paint_list(tmparray);
		//}
	});
}

function virtual_paint_list(data){
 var data_new=new Array();
 var j=0;
 for(var i=0;i<data.length;i++){
 	var ID = parseInt(parseInt(i)+1);
  	var mac = replacestr(data[i][11]);
  	var dip = data[i][4];
 	var sip = data[i][0];
 	if(dip=="any/0"){
 		dip="";
 		}
 	if(sip=="any/0"){
 		sip="";
 		}	
   if(data[i][8]=="5"){
  	var protocol = "ALL";
  }else if(data[i][8]=="1"){
  	var protocol = "TCP";
  }else if(data[i][8]=="2"){
  	var protocol = "UDP";
  }else if(data[i][8]=="4"){
  	var protocol = "ICMP";
  }else{
  	var protocol = "";
  	}
  var dfromport = data[i][6];
  var dtoport = data[i][7];
  var dport = dfromport +"-" +dtoport;
  var sfromport = data[i][2];
  var stoport = data[i][3];
  var sport = sfromport +"-" +stoport;
  if(data[i][9]=="0"){
  	var action = ports.filter_action_drop;
  }else if(data[i][9]=="1"){
  	var action = ports.filter_action_accept ;
  }else{
  	var action = "";
  	}
  var comment = data[i][10];
  data_new.push({ID:ID,mac:mac,dip:dip,sip:sip,protocol:protocol,dport:dport,sport:sport,action:action,comment:comment});
  j = i;
 }
 var fun_arr = [{type:"modify",name:"virtual_change"},{type:"del",name:"virtual_del"}];
 var tab = new libTable(new Array(firewall.no,ports.filter_macaddr,ports.filter_dest_ipaddr,ports.filter_source_ipaddr,firewall.protocol,ports.filter_dest_port_range,ports.filter_source_port_range,ports.filter_action,firewall.comment,msg.op),data_new,1,10);
 print_table("port_filter",tab,fun_arr, true);
}

function virtual_change(id,mac,dip,sip,protocol,dport,sport,action,comment)
{
 obj = virtual_data[parseInt(id)-1];
 old_virtual = parseInt(id)-1;
 with(obj){
  $("#mac").val(mac);
  if(dip=="any/0"){
 		dip="";
 		}
 	if(sip=="any/0"){
 		sip="";
 		}	
  $("#dip").val(dip);
  $("#sip").val(sip);
  if(protocol == "ALL"){
   listselect("protocol","5");
  }else if(protocol == "TCP"){
 	 listselect("protocol","1");
  }else if(protocol == "UDP"){
 	 listselect("protocol","2");
  }else if(protocol == "ICMP"){
 	 listselect("protocol","4");
  }
  if(dport.indexOf("-")>=0){
  	$("#dFromPort").val(dport.split("-")[0]);
  	$("#dToPort").val(dport.split("-")[1]);
  	}else if(dport!==""){
  		$("#dFromPort").val(dport);
  		$("#dToPort").val(dport);
  	}
  if(sport.indexOf("-")>=0){
  	$("#sFromPort").val(sport.split("-")[0]);
  	$("#sToPort").val(sport.split("-")[1]);
  	}else if(sport!==""){
  		$("#sFromPort").val(sport);
  		$("#sToPort").val(sport);
  	}
  	 if(action == ports.filter_action_drop){
  	action="0";
  }else if(action == ports.filter_action_accept){
  	action = "1" ;
  }else{
  	action = "";
  	}
  listselect("action",action);
  $("#comment").val(comment);
 }
 Modify_button();
}


function virtual_del(id,mac,dip,sip,protocol,dport,sport,action,comment){
 if(confirm(msg.confirm_del)){
// show_message("del",msg.deleting);
 virtual_data[parseInt(id)-1] = [];
 var resultStr = JSONTostr(virtual_data);
  var settext='&id2325='+resultStr;
  submitform(modules["set_data"],settext,"port_filtering_main_load");
  load_html();
 }
}
</script>
